<?php
namespace app\index\controller;
use app\common\controller\Api;

class CouponTrade extends Api
{

    /**
     * @var \app\index\model\CouponTrade
     */
    protected $model = null;

    public $primarykey = 'almm_user_id';

    public $table_prefix = 'CouponUserTrade.';

    public $table_join_prefix = 'CouponTrade.';

    public $state_num_to_text = [
        'tk_status'=>[
            3=>'订单结算',
            12=>'订单付款',
            13=>'订单失效',
            14=>'订单成功'
        ],
        'settle_status'=>[
            1=>'待确认',
            2=>'待结算',
            3=>'已结算',
            4=>'已失效'
        ]
    ];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('CouponTrade');
    }

    public function list(){
        $condition = [];
        list($filer_arr, $op_arr) = $this->_condition($condition);

        $condition = [];
        list($filer_arr_join, $op_arr_join)= $this->_join_condtion($condition);

        list($where, $sort, $order, $offset, $limit) = $this->buildparams($filer_arr, $op_arr);  

        $page_num = $this->request->get("page_num", 1);
        $page_size = $this->request->get("page_size", 20);

        $where_join = $this->buildparams_where($filer_arr_join, $op_arr_join, 'where', $this->table_join_prefix);

        $total = $this->model::hasWhere("UserTrade", $where_join)
            ->where($where)
            ->order($sort, $order)
            ->count();

        $list = $this->model::hasWhere("UserTrade", $where_join)
            ->field('`CouponTrade`.*, CouponUserTrade.*')
            ->where($where)
            ->order($sort, $order)
            ->limit($offset, $limit)
            ->with(['UserTrade', 'user', 'shareUser'])
            ->select();
        $this->listToArray($list);
        $list = $this->state_from_num_to_text($list);
        $result = array("total" => $total, "page_total"=> ceil($total/$page_size), "page_num"=>ceil($page_num), "page_size"=>ceil($page_size), "list" => $list);
        $this->success('success', $result);
        return true;
    }

    public function _condition($condition){
        $condition['channel_id'] = $this->request->param('channel_id');
        $condition['user_id'] = $this->request->param('user_id');
        $condition['share_user_id'] = $this->request->param('share_user_id');
        $condition['trade_id'] = $this->request->param('trade_id');
        $condition['settle_status'] = $this->request->param('settle_status');

        $filer_arr = [];
        $op_arr = [];
        if($condition['channel_id']){
            $filer_arr['channel_id'] = $condition['channel_id'];
        }
        if($condition['user_id']){
            $filer_arr['user_id'] = $condition['user_id'];
        }
        if($condition['share_user_id']){
            $filer_arr['share_user_id'] = $condition['share_user_id'];
        }
        if($condition['trade_id']){
            $filer_arr['trade_id'] = $condition['trade_id'];
        }
        if($condition['settle_status']){
            $filer_arr['settle_status'] = $condition['settle_status'];
        }
        return array($filer_arr, $op_arr);
    }

    public function _join_condtion($condition){
        $condition['keyword'] = $this->request->param('keyword');
        $condition['start_create_date'] = $this->request->param('start_create_date');
        $condition['end_create_date'] = $this->request->param('end_create_date');
        $condition['tk_status'] = $this->request->param('tk_status');
        $condition['start_earning_date'] = $this->request->param('start_earning_date');
        $condition['end_earning_date'] = $this->request->param('end_earning_date');

        $filer_arr = [];
        $op_arr = [];
        if($condition['keyword']){
            $filer_arr['item_title'] = $condition['keyword'];
            $op_arr['item_title'] = 'like';
        }
        if($condition['start_create_date'] || $condition['end_create_date']){
            $filer_arr['create_time'] = $condition['start_create_date'].','.$condition['end_create_date'];
            $op_arr['create_time'] = 'BETWEEN'; 
        }
        if($condition['start_earning_date'] || $condition['end_earning_date']){
            $filer_arr['earning_time'] = $condition['start_create_time'].','.$condition['end_create_time'];
            $op_arr['earning_time'] = 'BETWEEN'; 
        }
        if($condition['tk_status']){
            $filer_arr['tk_status'] = $condition['tk_status'];
        }
        return array($filer_arr, $op_arr);
    }
}
